#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <host.h>
#include <ants.h>
#include <config.h>
	
void showhost(struct hostent *host) {
	char **alias;
	char *addr_name;
	if(!host){
		printf("never find host : \n");
		return;
	}
	if(host->h_name)
		printf("Official host name : %s\n", host->h_name);
	printf("Name aliases : \n");
	for(alias = host->h_aliases; *alias; alias++)
		printf("%s \n",*alias);
	printf("IP address:\n");
	for(alias = host->h_addr_list; *alias; alias++) {
		addr_name = (char *)inet_ntoa(*(struct in_addr *)(*alias));
		if(addr_name)
			printf("%s \n",addr_name);
	}
}
	
int connect_host(struct mission *pms, unsigned short port) {
	int sd;
	struct sockaddr_in pin;
	struct hostent *h;
	
	bzero(&pin, sizeof(pin));
	if(!(h = pms->h)) {
		struct in_addr host_addr;
		if(inet_aton(pms->host, &host_addr)) {
			/**((int *)(&host_addr)) = htonl(*(int *)(&host_addr));*/
			pin.sin_addr = host_addr;
			goto ip_ok;
		}
		else if(!(h = gethostbyname(pms->host))) {
			printf("can't get host\n");
			return -1;
		}
		pms->h = h;
	}
#ifdef DEBUG
	showhost(h);
#endif
	
	pin.sin_addr = *((struct in_addr *)h->h_addr);
ip_ok:
	pin.sin_family = AF_INET;
	pin.sin_port = htons(port);
	
	if((sd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
		printf("can't create socket.\n");
		return -1;
	}
	if(connect(sd, (struct sockaddr*)&pin, sizeof(pin)) == -1) {
		printf("can't connect web.\n");
		return -1;
	}
	return sd;
}	
	
static int parse_url(char *url, char * res, int flag) {
	char *p;
	char *pn;
	int err = 0;
	int len;
	
	if((!url) || (!(*url)) || (!res))
		return -1;
	
	if(flag == FILENAME)
	{
		len = strlen(url);
		p = url + len;
		do
		{
			p--;
			if(p < url)
			{
				err = -1;
				break;
			}
		} while(*p != '/');

		if(err != -1)
		{
			if((*(p-1) == '/') || (*(p+1) == 0))
				strcpy(res, "index.html");
			else
				strcpy(res, ++p);
		}
	}
	else
	{
			if((p = strstr(url, "http://")))
			{
				p+= 7;
				if(flag == PORTNAME)
					strcpy(res, "80");
			}
			else if((p = strstr(url, "ftp://")))
			{
				p+= 6;
				if(flag == PORTNAME)
					strcpy(res, "21");
			}
			else
			{
				p = url;
				if(flag == PORTNAME)
					strcpy(res, "80");
			}
	
			pn = p;
			while(*pn)
			{
				if(*pn == ':')
				{
					if(flag == PORTNAME)
					{
						pn++;
						p = pn;
						while(*p)
						{
							if(*p == '/')
								break;
							p++;
						}
						if(*p)
						{
							strncpy(res, pn, p-pn);
							res[p-pn] = 0;
						}
						else
							strcpy(res, pn);
						break;
					}
					else if(flag == SITENAME)
						break;
				}
				
				if(*pn == '/')
					break;

				pn++;
			}

			if(flag == PATHNAME)
				strcpy(res, pn);
			else if(flag == SITENAME)
			{
				strncpy(res, p, pn-p);
				res[pn-p] = 0;
			}
	}
	return err;
}		
	
int fill_mission(struct mission *pms, char *name) {
	char temp[MAX_HOSTNAME];
	
	strcpy(pms->url, name);
	
	if(parse_url(name, temp, PORTNAME) == -1) {
		printf("bad url.\n");
		return 0;
	}
	pms->port = atoi(temp);
	
	if(parse_url(name, temp, SITENAME) == -1) {
		printf("bad url.\n");
		return 0;
	}
	strcpy(pms->host, temp);
	
	if(parse_url(name, temp, PATHNAME) == -1) {
		printf("bad url.\n");
		return 0;
	}
	strcpy(pms->path, temp);
	
	if(parse_url(name, temp, FILENAME) == -1) {
		printf("bad url.\n");
		return 0;
	}
	strcpy(pms->file, temp);
	
	if((!(*pms->referer)) && (pms->port == 80))
		strcpy(pms->referer, pms->url);
	return 1;
}	
